package weeny.pre.io;

import java.io.InputStream;
import java.io.OutputStream;

import prefuse.data.Graph;
import prefuse.data.Table;
import prefuse.data.io.DataIOException;

/**
 * Some utility functions to read or write tables in the xml format.
 * 
 * @author weeny
 *
 */
public class XmlTableUtil {
	public static Table readTable(InputStream is) {
		try {
			return new MyGraphReader().readGraph(is).getNodeTable();
		} catch (DataIOException e) {
			e.printStackTrace();
		}
		return null;
	}
	public static Table readTable(String fileName){
		try {
			return new MyGraphReader().readGraph(fileName).getNodeTable();
		} catch (DataIOException e) {
			e.printStackTrace();
		}
		return null;
	}
	public static void writeTable(Table table, OutputStream os){
		Graph g = new Graph(table, false);
		try {
			new MyGraphWriter().writeGraph(g, os);
		} catch (DataIOException e) {
			e.printStackTrace();
		}
	}
	public static void writeTable(Table table, String fileName){
		Graph g = new Graph(table, false);
		try {
			new MyGraphWriter().writeGraph(g, fileName);
		} catch (DataIOException e) {
			e.printStackTrace();
		}
	}
	public static Table[] readTables(String file){
		try {
			Graph g = new MyGraphReader().readGraph(file);
			Table[] result = new Table[2];
			result[0] = g.getNodeTable();
			result[1] = g.getEdgeTable();
			return result;
		} catch (DataIOException e) {
			e.printStackTrace();
		}
		return null;
	}
}